Skip to content

Use PIXEL_FORMAT_YUV420SP to fix phots purple/blue colour cast - #88

Draft
matt-t-needham wants to merge 2 commits into
alexey-pelykh:mainfrom
matt-t-needham:fix/photo-colour-nv21
Draft

Use PIXEL_FORMAT_YUV420SP to fix phots purple/blue colour cast#88
matt-t-needham wants to merge 2 commits into
alexey-pelykh:mainfrom
matt-t-needham:fix/photo-colour-nv21

Conversation

@matt-t-needham

Copy link
Copy Markdown

Problem

Photos taken with takePicture() have a purple or blue colour cast. Videos are fine.

Expected cause

a bug already documented in saki4510t/UVCCamera#253:
takePicture() requests frames via setFrameCallback(..., PIXEL_FORMAT_NV21). The native C library contains two YUYV conversions whose names and actual outputs are mismatched

uvc_yuyv2yuv420SP produces NV21 (V,U chroma byte order)
uvc_yuyv2iyuv420SP actually produces NV12 (U,V chroma byte order)
Requesting PIXEL_FORMAT_NV21 (= 5) routes to the function that produces NV12 data. Android's YuvImage(data, ImageFormat.NV21) then inverts U and V shifting reds to blue/purple in the output. also reported in in saki4510t/UVCCamera#567.

Fix

Change the pixel format constant from PIXEL_FORMAT_NV21 to PIXEL_FORMAT_YUV420SP in takePicture(). PIXEL_FORMAT_YUV420SP (= 4) routes to the function that produces correctly-ordered NV21 data. saveTakenPictureToFile() is unchanged.

Notes

If somebody hotfixed this already by post-processing photos to swap R/B channels they would see the inverse problem after this fix. Feels like it should be corrected, though?
This fix only affects the takePicture() path. The frame callback is removed immediately after capture and is never active during video recording, so I don't think it will affect that.

saki4510t's UVCCamera C lib has two YUYV conversion functions with similar names (see saki4510t#253):

uvc_yuyv2yuv420SP  — named YUV420SP, actually produces NV21 (V,U order)
uvc_yuyv2iyuv420SP — named with 'i', actually produces NV12 (U,V order)

Request PIXEL_FORMAT_YUV420SP (= 4) to get the NV21 data ordered correctly and correctly allocate colours.
@matt-t-needham
matt-t-needham marked this pull request as draft March 24, 2026 21:07
@matt-t-needham

Copy link
Copy Markdown
Author

made with robot supervision, still testing and verifying, limited device availability

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant